package com.atguigu.yygh.hosp.listener;

import com.atguigu.yygh.common.config.MqConst;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.mq.RabbitMQService;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.OrderMqVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

//@Component
public class OrderMqListener {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RabbitMQService rabbitMQService;
/*
* 确认挂号,走该方法
* 取消预约,走方法
* */
    @RabbitListener(bindings =
            @QueueBinding(
                    value = @Queue(name = MqConst.QUEUE_ORDER,durable = "true"), //创建队列
                    exchange =@Exchange(name = MqConst.EXCHANGE_DIRECT_ORDER),
                    key =  {MqConst.ROUTING_ORDER}
            ))
    public void consume(OrderMqVo orderMqVo, Message message, Channel channel) throws Exception{
        String scheduleId = orderMqVo.getScheduleId();              //获取排班Id
        Integer availableNumber = orderMqVo.getAvailableNumber();   //获取可预约数

        if (availableNumber !=null){
            boolean flag = scheduleService.updateAvailableNumber(scheduleId, availableNumber);
        }else{
//            取消预约
            scheduleService.cancelSchedule(scheduleId);
        }
        MsmVo msmVo = orderMqVo.getMsmVo();
        rabbitMQService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);
    }
}
